﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Xceed.Silverlight.Data;

namespace Xceed.Silverlight.DataGrid
{
  internal class VirtualGroupContainer : VirtualContainerBase, IRecyclable
  {
    public override UIElement Visual
    {
      get
      {
        return this.Headers.FirstOrDefault();
      }
    }

    public override Data.DataPath Path
    {
      get { return m_path; }
    }

    public double Indent { get; set; }

    internal bool IsRecycled
    {
      get
      {
        return m_isRecycled;
      }
      set
      {
        if( m_isRecycled != value )
        {
          m_isRecycled = value;
          this.UpdateVisibility();
        }
      }
    }

    internal object RecycleKey
    {
      get { return this.Path; }
    }

    public override void SetDataPath( DataPath path )
    {
      m_path = path;
    }

    internal override Size MeasurePanelOverride( Size availableSize )
    {
      availableSize.Width -= this.Indent;
      return base.MeasurePanelOverride( availableSize );
    }

    internal override Size ArrangePanelOverride( Size finalPosition, LayoutParameters parameters )
    {
      finalPosition.Width -= this.Indent;
      return base.ArrangePanelOverride( finalPosition, parameters );
    }

    protected override void ArrangeElement( ILayoutElement element, Rect finalRect, LayoutParameters parameters )
    {
      finalRect.X += this.Indent;
      base.ArrangeElement( element, finalRect, parameters );
    }

    internal override void Accept( IVisitor visitor )
    {
      visitor.Visit( this );
    }

    internal override void SetHeadersFooters( IEnumerable<HeaderFooterWrapper> headers, IEnumerable<HeaderFooterWrapper> footers )
    {
      base.SetHeadersFooters( headers, footers );
      this.UpdateVisibility();
    }

    private void UpdateVisibility()
    {
      Visibility visibility = ( this.IsRecycled ) ? Visibility.Collapsed : Visibility.Visible;
      this.ApplyToHeadersFooters( ( e ) => e.Visibility = visibility );
    }

    private DataPath m_path;
    private bool m_isRecycled;

    #region IRecyclable Members

    bool IRecyclable.IsRecycled
    {
      get
      {
        return this.IsRecycled;
      }
      set
      {
        this.IsRecycled = value;
      }
    }

    object IRecyclable.RecycleKey
    {
      get { return this.RecycleKey; }
    }

    #endregion
  }
}
